import java.util.LinkedList;

public class Board
{

	int		blockNumber	= 0;
	Block	myBoard[][];

	public Board()
	{

		myBoard = new Block[6][6];
	}

	/**
	 * author :nora createBoard : simply initialize a 2D array and inserts the
	 * blocks and this method is called in the engine
	 */

	public Block[][] createBoard()
	{

		insertMouse();
		insertHorizontal();
		insertVertical();
		return myBoard;

	}

	public void insertMouse()
	{
		int r = 2;
		int c = randomNumber(0, 4);
		if (c + 1 >= 6)
			insertMouse();
		else
		{
			LinkedList<Pair> loc = new LinkedList<Pair>();
			Pair firstLoc = new Pair(r, c);
			Pair secondLoc = new Pair(r, c + 1);
			loc.addLast(firstLoc);
			loc.addLast(secondLoc);
			Block mouse = new Block('M', loc, blockNumber);
			blockNumber++;
			myBoard[r][c] = mouse;
			myBoard[r][c + 1] = mouse;
		}
	}

	public void insertHorizontal()
	{
		for (int r = 0; r < 6; r++)
		{

			for (int numberOfBlocks = randomNumber(0, 3); numberOfBlocks < 3; numberOfBlocks++)
			{
				int c = randomNumber(0, 5);
				checkLocationForHorizontal(r, c);
			}

		}
	}

	public void insertVertical()
	{
		for (int c = 0; c < 6; c++)
		{

			for (int numberOfBlocks = randomNumber(0, 2); numberOfBlocks < 2; numberOfBlocks++)
			{
				int r = randomNumber(0, 5);
				checkLocationForVertical(r, c);
			}

		}
	}

	public void checkLocationForHorizontal(int row, int coloumn)
	{
		if (coloumn + 1 < 6 && myBoard[row][coloumn] == null
				&& myBoard[row][coloumn + 1] == null)
		{
			LinkedList<Pair> loc = new LinkedList<Pair>();
			Pair firstLoc = new Pair(row, coloumn);
			Pair secondLoc = new Pair(row, coloumn + 1);
			loc.addLast(firstLoc);
			loc.addLast(secondLoc);
			Block horizontal = new Block('H', loc, blockNumber);
			blockNumber++;
			myBoard[row][coloumn] = horizontal;
			myBoard[row][coloumn + 1] = horizontal;
		}
	}

	public void checkLocationForVertical(int row, int coloumn)
	{
		if (row + 1 < 6 && myBoard[row][coloumn] == null
				&& myBoard[row + 1][coloumn] == null)
		{
			LinkedList<Pair> loc = new LinkedList<Pair>();
			Pair firstLoc = new Pair(row, coloumn);
			Pair secondLoc = new Pair(row + 1, coloumn);
			loc.addLast(firstLoc);
			loc.addLast(secondLoc);
			int twoOrThree = randomNumber(2, 3);
			if (twoOrThree == 3 && row + 2 < 6
					&& myBoard[row + 2][coloumn] == null)
			{
				Pair ThirdLoc = new Pair(row + 2, coloumn);
				loc.addLast(ThirdLoc);
			}
			Block vertical = new Block('V', loc, blockNumber);
			blockNumber++;
			myBoard[row][coloumn] = vertical;
			myBoard[row + 1][coloumn] = vertical;
			if (twoOrThree == 3 && row + 2 < 6
					&& myBoard[row + 2][coloumn] == null)
			{
				myBoard[row + 2][coloumn] = vertical;
			}
		}
	}

	public int randomNumber(int min, int max)
	{
		int random = min + (int) (Math.random() * ((max - min) + 1));
		return random;
	}

	public void printBoard()
	{
		for (int i = 0; i < 6; i++)
		{
			for (int j = 0; j < 6; j++)
			{
				if (myBoard[i][j] == null)
				{
					System.out.print(" x ");
				}
				else
					System.out.print(" " + myBoard[i][j].blockNumber + " ");
			}
			System.out.println("");
		}
	}

	public Block getBlock(int row, int coloumn)
	{
		if (row > 6)
			System.out.println("row" + row);
		if (coloumn > 6)
			System.out.println("coloumn" + coloumn);
		return myBoard[row][coloumn];
	}

	public void removeBlock(int row, int coloumn)
	{
		myBoard[row][coloumn] = null;
	}

	public void addBlock(int row, int coloumn, Block blockToBeAdded)
	{
		myBoard[row][coloumn] = blockToBeAdded;
	}

	public int getBlockNumber()
	{
		return blockNumber;
	}

	public void setBlockNumber(int blockNumber)
	{
		this.blockNumber = blockNumber;
	}

	public Block[][] getMyBoard()
	{
		return myBoard;
	}

	public void setMyBoard(Block[][] myBoard)
	{
		this.myBoard = myBoard;
	}

	public Board copyBoard(Board boardToBeCopied)
	{
		Board copiedBoard = new Board();
		for (int i = 0; i < 6; i++)
		{
			for (int j = 0; j < 6; j++)
			{
				copiedBoard.myBoard[i][j] = boardToBeCopied.myBoard[i][j];
			}
		}
		return copiedBoard;
	}

	/**
	 * not taking into account any blocks or blanks
	 * 
	 * @param mouseLocation
	 * @return score
	 */
	public int h1(int mouseLocation)
	{
		int rightMostValue = mouseLocation;
		int score = 5 - rightMostValue;
		return score;
	}

	/**
	 * taking into account blanks
	 * 
	 * @param mouseLocation
	 * @return score
	 */
	public int h2(int mouseLocation, Board currentBoard)
	{
		int score = 0;
		for (int i = mouseLocation; i < 5; i++)
		{
			if (currentBoard.getBlock(i, 3) == null)
			{
				score++;
			}
		}
		return score;
	}

	public int getMouseLocation(Board currentB)
	{
		int x = 8;
		for (int i = 0; i < 6; i++)
		{
			for (int j = 0; j < 6; j++)
			{
				if (currentB.getBlock(i, j) != null
						&& currentB.getBlock(i, j).getState() == 'M')
				{
					x = currentB.getBlock(i, j).getLocation().getLast().getY();
				}
			}
		}
		return x;
	}
}
